using System.Collections.Generic;
using Game.Main.Model;
using SimpleJSON;
using UnityEngine;

namespace Game.Component
{
	public class StarItem : BaseItem
	{
		private const float EACH_WIDTH = 5.12f;

		private const float START_Y = 7f;

		private const float END_Y = 10f;

		private GameObject star1;

		private GameObject star2;

		private float width;

		private List<StarData> star1s = new List<StarData>();

		private List<StarData> star2s = new List<StarData>();

		private bool isVisible = true;

		public override void Init(ItemArchiveData data)
		{
			base.Init(data);
			star1 = model.Find("Star1").gameObject;
			star2 = model.Find("Star2").gameObject;
		}

		protected override void LateUpdate()
		{
			if (isVisible)
			{
				UpdateStartState(star1s);
				UpdateStartState(star2s);
			}
		}

		public void UpdateStartState(List<StarData> list)
		{
			for (int i = 0; i < list.Count; i++)
			{
				StarData starData = list[i];
				if (!starData.gameObject.activeSelf)
				{
					continue;
				}
				if (starData.animTime > 0f)
				{
					starData.animTime -= Time.deltaTime / 2f;
					if (starData.animTime <= 0f)
					{
						starData.animTime = 0f;
					}
					if (starData.appear)
					{
						starData.sprite.color = new Color32(byte.MaxValue, byte.MaxValue, byte.MaxValue, (byte)(255f * starData.animTime));
					}
					else
					{
						starData.sprite.color = new Color32(byte.MaxValue, byte.MaxValue, byte.MaxValue, (byte)(255f * (1f - starData.animTime)));
					}
					if (starData.animTime <= 0f)
					{
						starData.appear = !starData.appear;
						starData.ResetWaitTime();
					}
				}
				else
				{
					starData.waitTime -= Time.deltaTime;
					if (starData.waitTime <= 0f)
					{
						starData.ResetAnimTime();
					}
				}
			}
		}

		public void InitWidth(float layerWidth)
		{
			width = layerWidth;
		}

		public void InitStars()
		{
			float num = -5.12f;
			float num2 = num + 5.12f;
			int num3 = 0;
			int num4 = 0;
			while (num2 < width)
			{
				num += 5.12f;
				num2 += 5.12f;
				if (num2 >= width)
				{
					num2 = width;
				}
				int num5 = Random.Range(0, 3);
				float num6 = 0f;
				for (int i = 0; i < num5; i++)
				{
					num6 = Random.Range(num, num + 5.12f);
					if (num6 <= num2)
					{
						Transform transform;
						if (num3 < star1s.Count)
						{
							transform = star1s[num3].transform;
							star1s[num3].Init();
						}
						else
						{
							transform = CreateStar(star1);
							star1s.Add(new StarData(transform));
						}
						transform.transform.localPosition = new Vector3(num6, Random.Range(7f, 10f), 0f);
						num3++;
					}
				}
				num5 = Random.Range(2, 6);
				for (int j = 0; j < num5; j++)
				{
					num6 = Random.Range(num, num + 5.12f);
					if (num6 <= num2)
					{
						Transform transform;
						if (num4 < star2s.Count)
						{
							transform = star2s[num4].transform;
							star2s[num4].Init();
						}
						else
						{
							transform = CreateStar(star2);
							star2s.Add(new StarData(transform));
						}
						transform.transform.localPosition = new Vector3(num6, Random.Range(7f, 10f), 0f);
						num4++;
					}
				}
			}
			if (num3 < star1s.Count - 1)
			{
				for (int k = num3 + 1; k < star1s.Count; k++)
				{
					star1s[k].transform.gameObject.SetActive(false);
				}
			}
			if (num4 < star2s.Count - 1)
			{
				for (int l = num4 + 1; l < star2s.Count; l++)
				{
					star2s[l].transform.gameObject.SetActive(false);
				}
			}
		}

		public void SetStarVisible(bool val)
		{
			isVisible = val;
			model.gameObject.SetActive(val);
		}

		private Transform CreateStar(GameObject demo)
		{
			Transform transform = Object.Instantiate(demo).transform;
			transform.SetParent(model);
			transform.gameObject.SetActive(true);
			return transform;
		}

		public override void PackData(JSONObject obj)
		{
			base.PackData(obj);
		}
	}
}
